package com.neusoft.demo.component;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class loginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object user = request.getSession().getAttribute("account");     //取出session内的account，登陆成功会有值

        if(user == null){
            request.setAttribute("msg", "无权限，请先登录");
            request.getRequestDispatcher("/toLogin").forward(request, response);
            //返回登录界面
            return false;
        }

        System.out.println(request.getSession().getAttribute("account"));

        return true;
    }
}
